package com.beyondbit.demo.controller;

import com.beyondbit.demo.entity.Dictionary;
import com.beyondbit.demo.entity.query.DictionaryQuery;
import com.beyondbit.demo.service.DictionaryService;
import com.beyondbit.web.infrastructure.controller.BaseApiController;
import com.beyondbit.web.infrastructure.entity.Page;
import com.beyondbit.web.infrastructure.model.JsonResultMessage;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import lombok.var;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lfx
 * @version 1.0
 * @date 2021/12/6 13:22
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("dic")
public class DictionaryController  extends BaseApiController {
    /**
     * 服务对象
     */
    @Resource
    private DictionaryService dictionaryService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Dictionary selectOne(String id) {
        return this.dictionaryService.queryById(id);
    }

    @GetMapping("query")
    public JsonResultMessage<Page<Dictionary>> query(DictionaryQuery query) {
        val userPage = dictionaryService.query(query);

        return success(userPage);
    }


    public JsonResultMessage<Dictionary> get(String id) {
        var user = dictionaryService.queryById(id);

        return success(user);
    }


    public JsonResultMessage add(Dictionary dictionaryModel) {
        try {

            dictionaryService.insert(dictionaryModel);

            return success("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return apiErrorResult(e);
        }
    }


    public JsonResultMessage update(String id, Dictionary dictionaryModel) {

        dictionaryModel.setDicId(id);
        dictionaryService.update(dictionaryModel);

        return success("修改成功");
    }


    public JsonResultMessage delete(String id) {
        dictionaryService.deleteById(id);

        return success("删除成功");
    }


    public JsonResultMessage dictionarytree() {
        String treeid="1";
        List<Dictionary> nodeTree = dictionaryService.getNodeTree(treeid);
        Object menuTree = dictionaryService.getDicTree(nodeTree);
        return success(menuTree);
    }
}